package cn.jimmie.learn.viewArt.weight

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import cn.jimmie.learn.viewArt.R

/**
 * function :
 *
 *
 *  Create Time:  2019年01月16日 19:05
 *
 *  @author Jimmie.Qian
 */
class EasyCircleLoadingView(context: Context, attrs: AttributeSet) : BaseView(context, attrs),
    ValueAnimator.AnimatorUpdateListener {

    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val mAnimator = ValueAnimator.ofInt(0, Integer.MAX_VALUE)
    private var mDotRadius: Float
    private var mOffset = 0
    private var mLoadUnit = 1000f
    private val mMatrix = Matrix()

    init {

        @SuppressLint("CustomViewStyleable")
        val ta = context.obtainStyledAttributes(attrs, R.styleable.CircleLoadingView)
        val padding = ta.getDimension(R.styleable.CircleLoadingView_android_padding, 0f)
        val realH = h - padding * 2
        val dDotRadius = realH / 8f
        mDotRadius = ta.getDimension(R.styleable.CircleLoadingView_dotRadius, dDotRadius)
//        val radius = ta.getDimension(R.styleable.CircleLoadingView_radius, realH / 2 - mDotRadius * 8)
        val color = ta.getColor(R.styleable.CircleLoadingView_dotColor, Color.BLACK)
        ta.recycle()

        mPaint.color = color

        mAnimator.interpolator = LinearInterpolator()
        mAnimator.duration = Integer.MAX_VALUE.toLong()
        mAnimator.addUpdateListener(this)
        mAnimator.start()
    }

    override fun onAnimationUpdate(animation: ValueAnimator) {
        val value = animation.animatedValue as Int % mLoadUnit
        val offset = (value / (mLoadUnit / 8)).toInt()
        // 性能优化, 避免不必要的绘制
        if (mOffset != offset) {
            mOffset = offset
            invalidate()
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        for (i in 0..8) {
            val index = 8 - (mOffset + i) % 8
            val s = index / 8f
            drawCanvasMatrix(canvas, s)
        }
    }

    private fun drawCanvasMethod(canvas: Canvas, s: Float) {
        canvas.rotate(45f, width / 2f, height / 2f)
        // 画布操作实现
        canvas.save()
        canvas.clipRect(width / 2f - mDotRadius, 0f, width / 2f + mDotRadius, mDotRadius * 2)
        canvas.scale(s, s, width / 2f, mDotRadius)
        canvas.drawCircle(width / 2f, mDotRadius, mDotRadius, mPaint)
        canvas.restore()
    }

    private fun drawCanvasCalculate(canvas: Canvas, s: Float) {
        canvas.rotate(45f, width / 2f, height / 2f)
        // 计算实现
        val radius = mDotRadius * s
        canvas.drawCircle(width / 2f, mDotRadius, radius, mPaint)
    }

    private fun drawCanvasMatrix(canvas: Canvas, s: Float) {
        mMatrix.reset()
        mMatrix.setRotate(45f, width / 2f, height / 2f)
        canvas.concat(mMatrix)

        canvas.save()
        canvas.clipRect(width / 2f - mDotRadius, 0f, width / 2f + mDotRadius, mDotRadius * 2)

        mMatrix.reset()
        mMatrix.setScale(s, s, width / 2f, mDotRadius)
        canvas.concat(mMatrix)

        canvas.drawCircle(width / 2f, mDotRadius, mDotRadius, mPaint)
        canvas.restore()
    }

    override fun onDetachedFromWindow() {
        mAnimator.cancel()
        super.onDetachedFromWindow()
    }

    fun setSpeed(speed: Float) {
        mLoadUnit *= speed
    }

    override fun setVisibility(visibility: Int) {
        mAnimator.cancel()
        if (visibility == View.VISIBLE) {
            mAnimator.start()
        }
        super.setVisibility(visibility)
    }
}
